<template>
  <div class="employee-container">
    <div class="input-suffix">
      <span>员工姓名：</span>
      <el-input v-model="params.name" placeholder="请输入内容" class="search-main" />
      <el-button type="primary">查询</el-button>
    </div>

    <div class="create-container">
      <el-button type="primary" class="add-container" @click="open">添加员工</el-button>
      <el-table
        :data="employeeList"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
          width="180"
        />
        <el-table-column
          prop="name"
          label="员工姓名"
          width="180"
        />
        <el-table-column
          prop="userName"
          label="登录账号"
        />
        <el-table-column
          prop="phonenumber"
          label="联系方式"
        />
        <el-table-column
          prop="roleName"
          label="角色"
        />
        <el-table-column
          prop="status"
          label="状态"
        />
        <el-table-column
          prop="createTime"
          label="添加时间"
        />
        <el-table-column
          label="操作"
        >
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-container">
      <div class="block">
        <el-pagination
          :page-size="params.pageSize"
          layout="total, prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      title="添加员工"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserListAPI, createEmployeeAPI } from '@/api/user'
import { getRoleListAPI } from '@/api/system'
export default {
  name: 'Employee',
  data() {
    return {
      params: {
        name: '',
        page: 1,
        pageSize: 2
      },
      employeeList: [],
      roleList: [],
      total: 0,
      dialogVisible: false,
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      console.log(this.params)
      const res = await getUserListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getUserList()
    },
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + 1 + index
    },
    async open() {
      this.dialogVisible = true
      const res = await getRoleListAPI()
      this.roleList = res.data
    },
    confirmAdd() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        await createEmployeeAPI(this.addForm)
        this.dialogVisible = false
        await this.getUserList()
        this.$refs.addForm.resetFields()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.input-suffix {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 1px solid rgb(237, 237, 237, .9);

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  .add-container {
    margin: 15px 0;
  }
}

.page-container {
  padding: 4px 0;
  text-align: right
}
</style>
